import pandas as pd
import os

# replacement_values = [
#     5852507880.333229, 1912138912041.068848, 192162603449.710999, 
#     257351173124.112274, 776474415494.436279, 333459250881.328735, 
#     311909174006.904968, 556580037226.484863, 15373047453.513386, 
#     363267475370.696106
# ]
# input_folder=""

# for file in input_folder:
#     if file.endswith(".csv"):
#         df=pd.read_csv(file)

def calculate_new_columns(area_csv_path,input_folder,output_csv_path):
    #读取area表，将其中数据设置为合适的类型
    area_df=pd.read_excel(area_csv_path,usecols=['COUNTRY','AREA'])
    area_df['AREA']=area_df['AREA'].apply(pd.to_numeric)
    area_dict=area_df.set_index("COUNTRY")["AREA"].to_dict()


    result_df=area_df.copy()
    #读取arcgis pro输出的统计表并开始计算
    for file in os.listdir(input_folder):
        if file.endswith(".csv"):
            file_path=os.path.join(input_folder,file)
            try:
                data_df=pd.read_csv(file_path,usecols=['COUNTRY','MEAN'])
            except Exception as e:
                print(f"无法读取文件{file}:{e}")
                continue
            file_date=file.split("_")[2]

            #计算每个国家的月碳排放量
            data_df['AREA']=data_df['COUNTRY'].map(area_dict)
            data_df[file_date]=data_df['MEAN']*data_df['AREA']

            #合并结果
            result_df=result_df.merge(data_df[['COUNTRY',file_date]],on='COUNTRY',how='left')
    result_df.to_csv(output_csv_path,index=False)
    print(f"结果已保存到{output_csv_path}")


area_csv_path=r"D:\XiaLei_data\original_data\area.xlsx"
input_folder=r"D:\XiaLei_data\output_data\csv\GFED\2013"
output_csv_path=r"D:\XiaLei_data\output_data\csv\GFED\2013\GFED2013_汇总.csv"
calculate_new_columns(area_csv_path,input_folder,output_csv_path)
    
        
